import java.io.File;

import org.apache.commons.lang.StringUtils;


public class FileEx {

	public static void main(String[] args) {

		testFile("c:/images/image1.tif");
		testFile("c:/images/non-existing-file.txt");
		//testFile("//starsimages/some_file.000");
		testFile("//vitech-projdata/Common/8148");
		
		String fullPath = "//vitech-projdata/Common/8148";
		String parentPath = null;

		parentPath = extractParentPath(fullPath);
		System.out.println("full path  : " + fullPath);
		System.out.println("parent path: " + parentPath);
		System.out.println("---------------------");
		
		fullPath = "c:/images/image1.tif";
		parentPath = extractParentPath(fullPath);
		System.out.println("full path  : " + fullPath);
		System.out.println("parent path: " + parentPath);
		System.out.println("---------------------");
		
		fullPath = "\\\\starsimages\\image.000";
		parentPath = extractParentPath(fullPath);
		System.out.println("full path  : " + fullPath);
		System.out.println("parent path: " + parentPath);
		System.out.println("---------------------");
		
		fullPath = "\\\\starsimages";
		parentPath = extractParentPath(fullPath);
		System.out.println("full path  : " + fullPath);
		System.out.println("parent path: " + parentPath);
		System.out.println("---------------------");
		
	}

	private static void testFile(String pathname) {
		File f = new File(pathname);
		System.out.println("path name: " + pathname);
		System.out.println("exists? " + f.exists());
		System.out.println("parent path: " + f.getParent());
		System.out.println("name: " + f.getName());
		System.out.println("---------------------");
	}
	
	private static String extractParentPath(String pathname) {
		if (StringUtils.isBlank(pathname))
			return pathname;
		
		String path = pathname.replace('\\', '/');
		int lastSlashIndex = path.lastIndexOf('/');
		if (lastSlashIndex > -1)
			return path.substring(0, lastSlashIndex);
		return path;
	}
}
